package com.evan.kdl.test.day02;

import cn.hutool.core.date.StopWatch;
import com.evan.kdl.test.entity.MyTask;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SequenceDemo {
    public static void main(String[] args) {
        // 方案一：在主线程中使用串行执行
        // step 1: 创建10个MyTask对象,每个任务持续1s,存入list集合便于启动Stream操作
        IntStream intStream = IntStream.range(0, 18);
        List<MyTask> tasks = intStream.mapToObj(item -> {
            return new MyTask(1);
        }).collect(Collectors.toList());

        // step 2: 执行tasks集合中的每个任务，统计总耗时
        StopWatch sw = new StopWatch();
        sw.start();
        List<Integer> result = tasks.stream().map(myTask -> {
            return myTask.doWork();
        }).collect(Collectors.toList());
        sw.stop();
        System.out.printf("processed %d tasks cost %.2f second", tasks.size(), sw.getTotalTimeSeconds());
        result.forEach(System.err::println);
    }
}